// pages/brochure/byxItem/item5.js
const app = getApp();
//引入图片预加载组件
const ImgLoader = require('../../img-loader/img-loader.js');
const imgUrlOriginal = app.baseUrl + 'images/brochure/byx_item5.png?v=' + app.globalData.version;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent',
      isShare: 0
    },
    requestUrl: app.baseUrl,
    shareTitle: '',
    shareImg: '',

    baseUrl: app.baseUrl,
    videoUrl: app.videoUrl,
    version: app.globalData.version,
    hide1: 'none',
    hide2: 'none',
    itemBg: '',
    showLoading: true,
    showImgList: [],
    openid: ''
  },
  // 监听页面滚动事件
  onPageScroll: function (e) {
    var that = this;


    var p = e.scrollTop / 100;
    console.log(p);
    if (p > 21) { //这里100代表你要动画的元素离最顶层的距离，console.log一下就知道了。

      that.setData({
        hide1: 'block'
      })
    }
    if (p > 50) { //这里100代表你要动画的元素离最顶层的距离，console.log一下就知道了。

      that.setData({
        hide2: 'block'
      })
    }

  },
  moreHouseType() {
    wx.navigateTo({
      url: '/pages/brochure/byxItem/houserType',
    })
  },

  goHouseTypeBig(e) {
    var that = this;
    var typeId = e.currentTarget.dataset.typeid;
    var currentUrl = that.data.baseUrl + "images/brochure/" + typeId + ".png";
    var showImgList = that.data.showImgList;
    console.log("currentUrl:", currentUrl)
    showImgList[0] = currentUrl;
    wx.previewImage({
      current: currentUrl, // 当前显示图片的http链接
      urls: showImgList // 需要预览的图片http链接列表
    })
  },
  //获取微信分享信息
  getShareInfo(openid) {
    var that = this;
    wx.request({
      url: that.data.requestUrl + 'getLsShareInfo.php',
      method: 'POST',
      data: {
        type: 3,
        team: '碧影溪',
        openid: openid,
        id:8
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function (res) {}
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    var is_share = options.is_share;
    //判断返回的跳转路径
    if (is_share == 1) {
      this.setData({
        ['defaultData.isShare']: 1
      });
    }
    //初始化图片预加载组件
    this.imgLoader = new ImgLoader(this)
    this.loadImages();

    //获取缓存中的openid
    var openid = wx.getStorageSync('openid');

    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.requestUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                //缓存微信openid
                wx.setStorageSync('openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //调用分享
                that.getShareInfo(res.data.openid);
              },
              fail: function (res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //调用分享
      that.getShareInfo(openid);
    }
  },
  loadImages() {
    //同时对原图进行预加载，加载成功后再替换
    this.imgLoader.load(imgUrlOriginal, (err, data) => {
      console.log('图片加载完成', err, data.src)
      if (!err)
        this.setData({
          itemBg: data.src,
          showLoading: false
        })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/brochure/menu/index?is_share=1',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  },
  // 分享朋友圈
  onShareTimeline() {
    var that = this;
    return {
      title: that.data.shareTitle,
      query: 'is_share=1',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  }
})